home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbobug.arc / TURBOBUG.TXT < prev   
Text File  |  1989-03-19  |  6KB  |  144 lines

  1.  
  2.                     ***** BUG REPORT *****
  3.          ***** MAJOR BUG IN TURBO PASCAL V2.00 *****
  4.                          July 1,1984
  5.  
  6.  
  7.      The runtime routines  do  not  handle  a  floating-point 
  8. (real)  subtraction  correctly.  For  some subtractions,  the 
  9. correct  difference  is  returned;  for  others,  a  zero  is 
  10. returned. The following program demonstrates the bug:
  11.  
  12. program test;
  13. begin
  14.      writeln(9.+(-6.0));        { Wrong value returned }
  15.      writeln(-1.0+(-1.0));      { Correct value returned }
  16.      writeln(1-2);              { Correct value returned }
  17.      writeln(1.-2:10:2);        { Wrong value returned }
  18.      writeln(1.-2.0);           { Wrong value returned }
  19.      writeln(1-2.0);            { Wrong value returned }
  20.      writeln(456 - 123.0);      { Correct value returned }
  21. end.
  22.  
  23. A value of zero is returned on those lines marked as 'wrong'. 
  24. The other lines return the correct difference.
  25.  
  26.  
  27. THE CAUSE
  28.  
  29.     After disassembling and tracing through a sample compiled 
  30. program, the error was located in the following code:
  31.  
  32. XXXX:12FA E84FFF        CALL    124C            ; Subtract
  33. XXXX:12FD 7306          JNB     1305
  34. XXXX:12FF 80F780        XOR     BH,80           ; Handle negative
  35. XXXX:1302 E863FF        CALL    1268            ;   numbers
  36. XXXX:1305 8B4504        MOV     AX,[DI+04]      ; Is mantissa zero?
  37. XXXX:1308 0B4502        OR      AX,[DI+02]
  38. XXXX:130B 0A4501        OR      AL,[DI+01]      ; ***** ERROR *****
  39. XXXX:130E 740D          JZ      131D            ; Yes
  40. XXXX:1310 F6450580      TEST    BYTE PTR [DI+05],80  ; Normalize
  41. XXXX:1314 750C          JNZ     1322
  42.  
  43. (Comments  have  been  added for clarity).  This disassembled 
  44. code is located in the  routines  that  handle  addition  and 
  45. subtraction  (only the subtraction part is shown).  The error 
  46. occurs when the routine tests to see if  the  result  of  the 
  47. subtraction is zero. It tests for a zero by "OR-ing" together 
  48. the five bytes of the mantissa (the instructions that do this 
  49. are at offsets 1305H to 130BH). If the mantissa is zero, then 
  50. the result of this "multiple-or" will set the zero flag.  The 
  51. first  of  these instructions is a move of the word at [DI+4] 
  52. to AX.  The next instruction takes the logical OR of  AX  and 
  53. [DI+2].  No problem so far.  However, the last instruction is 
  54. "OR AL,[DI+1]",  an instruction that operates on a  byte  and 
  55. not  on  a word.  If the OR of the words at [DI+4] and [DI+2] 
  56. results in a word whose UPPER byte is NONZERO but whose LOWER 
  57. byte is ZERO,  then the next instruction, the "byte-wise" OR, 
  58. will SET the zero flag if the byte at [DI+1] is zero.  Notice 
  59. that the instruction at 130BH totally ignores the contents of 
  60. the upper byte of AX;  the flags are  set  according  to  the 
  61. lower byte of AX only.  This is what causes the error.
  62.  
  63.  
  64. THE FIX
  65.  
  66.      The fix to this bug is simple: simply exchange the order 
  67. of the two "OR" instructions.  This way, the zero flag is set 
  68. according  to  a  full  16-bit  OR  and  not  an  8-bit  one. 
  69. IMPORTANT:  Only persons familiar with the operation of DEBUG 
  70. should  attempt  the  following.  Using DEBUG,  the following 
  71. sequence of commands can be used to fix the bug:
  72.  
  73.      Assumptions and notes in the following:
  74.           1) ONLY WORK ON A COPY OF TURBO!  DO  NOT  USE 
  75.              YOUR MASTER COPY!
  76.           2) The sequence of commands shown below writes 
  77.              out  the   fixed   version   to   the  file 
  78.              'turbo.com'
  79.           3) Make sure that you have version 2.00
  80.           4) 'turbo.com'   must   be   in   the  current 
  81.              directory on drive B.
  82.           5) DOS  2.00  or  above  is  being  used  (the 
  83.              debugger in DOS 1.00 and 1.10 does not have 
  84.              the 'assemble' command).                           
  85.           6) Be  sure  to  verify  that  the code in the 
  86.              compiler is the same as  that  shown  below 
  87.              initially.  After the changes are made,  be 
  88.              sure to verify that the changes  have  been 
  89.              made  correctly  before  writing  the fixed 
  90.              version out to disk.
  91.  
  92. B>debug turbo.com
  93. -u 12fa             <- Verify the following locations
  94. XXXX:12FA E84FFF        CALL    124C
  95. XXXX:12FD 7306          JNB     1305
  96. XXXX:12FF 80F780        XOR     BH,80
  97. XXXX:1302 E863FF        CALL    1268
  98. XXXX:1305 8B4504        MOV     AX,[DI+04]
  99. XXXX:1308 0B4502        OR      AX,[DI+02]
  100. XXXX:130B 0A4501        OR      AL,[DI+01]
  101. XXXX:130E 740D          JZ      131D
  102. XXXX:1310 F6450580      TEST    BYTE PTR [DI+05],80
  103. XXXX:1314 750C          JNZ     1322
  104. XXXX:1316 E8F9FE        CALL    1212
  105. XXXX:1319 FE0D          DEC     BYTE PTR [DI]
  106. -a 1308             <- Make changes
  107. XXXX:1308 or al,[di+1]
  108. XXXX:130E or ax,[di+2]
  109. XXXX:1311                       <- Press 'enter'
  110. -u 12fa             <- Verify that the changes are correct
  111. XXXX:12FA E84FFF        CALL    124C
  112. XXXX:12FD 7306          JNB     1305
  113. XXXX:12FF 80F780        XOR     BH,80
  114. XXXX:1302 E863FF        CALL    1268
  115. XXXX:1305 8B4504        MOV     AX,[DI+04]
  116. XXXX:1308 0A4501        OR      AL,[DI+01]
  117. XXXX:130B 0B4502        OR      AX,[DI+02]
  118. XXXX:130E 740D          JZ      131D
  119. XXXX:1310 F6450580      TEST    BYTE PTR [DI+05],80
  120. XXXX:1314 750C          JNZ     1322
  121. XXXX:1316 E8F9FE        CALL    1212
  122. XXXX:1319 FE0D          DEC     BYTE PTR [DI]
  123. -w                  <- Save fixed version of turbo
  124. Writing 8E80 bytes
  125. -q
  126.  
  127. B>
  128.  
  129. <end of fix>
  130.  
  131.  
  132. OTHER NOTES ABOUT TURBO
  133.  
  134.      One  cannot  set  breakpoints  in TURBO using DEBUG.  It 
  135. seems that TURBO  uses  the  breakpoint  interrupt  for  some 
  136. hideous  reason.  If  one  attempts  to set breakpoints,  the 
  137. system will probably crash.  Tracing,  however,  does seem to 
  138. work.  The  above  bug  was  tracked down only after hours of 
  139. tracing (by maching and by hand) and disassembling.
  140.  
  141.  
  142. 
  143.  
  144. k.  The  above  bug  was  tracked down only